import request from '@/utils/request'
const scope = 'server'
const baseURL = process.env.VUE_APP_LOGIN_BASE_API
import { loginAuthorization } from '@/utils/auth'

export function login(username, password, code, randomStr) {
  const grant_type = 'password'
  let datas = { username, password, grant_type, scope }
  if (code !== '' && randomStr !== '') {
    datas = { username, password, randomStr, code, grant_type, scope }
  }
  return request({
    baseURL,
    url: '/auth/oauth/login',
    method: 'post',
    params: datas,
    headers: {
      isToken: false,
      'Authorization': `Basic ${loginAuthorization}`
    }
  })
}

export const refreshToken = (refresh_token) => {
  const grant_type = 'refresh_token'
  return request({
    baseURL,
    url: '/auth/oauth/token',
    headers: {
      'isToken': false,
      'Authorization': `Basic ${loginAuthorization}`
    },
    method: 'post',
    params: { refresh_token, grant_type, scope }
  })
}
// 获取用户信息
export function getInfo() {
  return request({
    baseURL,
    url: '/ucenter/api/v2/user/current',
    method: 'get'
  })
}
// 退出登录
export function logout() {
  return request({
    baseURL,
    url: '/auth/token/logout',
    method: 'delete'
  })
}

// 获取门户菜单
export function gePortalUserMenu() {
  return request({
    baseURL,
    url: '/ucenter/api/v2/portal/user/menu',
    method: 'get'
  })
}
/** 获取用户缓存 */
export function userCache() {
  return request({
    url: '/call-center/api/v1/sys/userCache',
    method: 'GET'
  })
}

// 检验当前用户前端URL合法性
export function validPortalFrontUrl({ frontUrl, menuCode }) {
  return request({
    url: `/ucenter/api/v2/portal/validFrontUrl`,
    method: 'get',
    params: { frontUrl, menuCode }
  })
}

// 根据账号获取办事处信息
export function officeInfo(account) {
  return request({
    url: `/ucenter/api/v2/organization/agencies?account=${account}`,
    method: 'GET'
  })
}

/** 获取办事处信息 */
export function agencies() {
  return request({
    url: '/call-center/api/v1/staffInfo/agencies',
    method: 'GET'
  })
}

/** 获取组织信息 */
export function org() {
  return request({
    url: '/call-center/api/v1/staffInfo/org',
    method: 'GET'
  })
}
